package com.tiger.ebenv.sample2;

import java.util.*;
import javax.naming.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.ejb.*;

public class WebMailBean implements javax.ejb.SessionBean {
    private javax.ejb.SessionContext sessionContext;
    public void setSessionContext(javax.ejb.SessionContext sessionContext) {
        this.sessionContext = sessionContext;
    }
    public void ejbCreate() throws javax.ejb.CreateException {
    }
    public void sendMail(String to, String subject, String message) {
        try {
            Context initial = new InitialContext();
            Session session = (Session) initial.lookup("java:comp/env/mail/MailSession");
            
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom();
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
            msg.setSubject(subject, "ISO-2022-JP");
            msg.setText(message, "ISO-2022-JP");
            msg.setHeader("X-Mailer", "WebMail");
            msg.setSentDate(new Date());
            Transport.send(msg);
            
        } catch(Exception e) {
            throw new EJBException(e.getMessage());
        }
    }
    public void ejbActivate() {}
    public void ejbPassivate() {}
    public void ejbRemove() {}
}
